﻿Imports System
Imports System.Security.Cryptography
Imports System.Text


Public Class Authentication

	Public Shared Function AutorizeEditOfficer(ByVal roleId As String) As List(Of Entity.AuthorizationEntityItem)
		Return DL.Authentication.LoadAutorizeEntity(roleId, "EDIT_OFFICER")
	End Function

	Public Shared Sub LogInLog(ByVal ipAddress As String, ByVal userId As String)
		DL.Authentication.LogInLog(ipAddress, userId)
	End Sub

	Public Shared Function getMd5Hash(ByVal input As String) As String
		' Create a new instance of the MD5CryptoServiceProvider object.
		Dim md5Hasher As New MD5CryptoServiceProvider()

		' Convert the input string to a byte array and compute the hash.
		Dim data As Byte() = md5Hasher.ComputeHash(Encoding.Default.GetBytes(input))

		' Create a new Stringbuilder to collect the bytes
		' and create a string.
		Dim sBuilder As New StringBuilder()

		' Loop through each byte of the hashed data 
		' and format each one as a hexadecimal string.
		Dim i As Integer
		For i = 0 To data.Length - 1
			sBuilder.Append(data(i).ToString("x2"))
		Next i

		' Return the hexadecimal string.
		Return sBuilder.ToString()

	End Function

	Public Shared Function LoadReport(ByVal userId As String) As DataTable
		Return DL.Authentication.LoadReport(userId)
	End Function

	Public Shared Function LoadDownload(ByVal userId As String) As DataTable
		Return DL.Authentication.LoadDownload(userId)
	End Function

    Public Shared Function LoadAdmin(ByVal userId As String) As DataTable
        Return DL.Authentication.LoadAdmin(userId)
    End Function

	Public Shared Function CheckPermissionAccessUnit(ByVal userId As String, ByVal unitID As String) As Boolean
		Dim dtTemp As DataTable = DL.Authentication.LoadFunction(userId, unitID)

		If dtTemp IsNot Nothing AndAlso dtTemp.Rows.Count > 0 Then
			CheckPermissionAccessUnit = True
		Else
			CheckPermissionAccessUnit = False
		End If

		Try
			If dtTemp IsNot Nothing Then dtTemp.Dispose()
		Catch
		End Try
	End Function

    Public Shared Function LoadUserIDInfo(ByVal officerappid As String) As DataTable
        Return DL.Authentication.LoadUserIDInfo(officerappid)
    End Function

    Public Shared Function LoadActiveFunction(ByVal userid As String) As DataTable
        Return DL.Authentication.LoadFunctionAuthorize(userid)
    End Function

    Public Shared Function LoadActiveRole(ByVal userid As String) As DataTable
        Return DL.Authentication.LoadActiveRole(userid)
    End Function

    Public Shared Function UpdateUserAuthorize(ByVal oUserRoleList As List(Of Entity.UserRowItem), ByVal oUserPrivilegeList As List(Of Entity.UserPrivilegeItem), ByVal userid As String) As Boolean
        Dim rslt As Boolean
        rslt = True
        Dim dalAuth As New DL.Authentication(True)

        Try
            rslt = dalAuth.DeleteUserRole(userid)
            If rslt Then
                rslt = dalAuth.DeleteUserPrivilege(userid)
                If rslt Then
                    For Each obj As Entity.UserRowItem In oUserRoleList
                        rslt = dalAuth.InsertUserRole(obj)
                        If Not rslt Then Exit For
                    Next
                    If rslt Then
                        For Each objPri As Entity.UserPrivilegeItem In oUserPrivilegeList
                            rslt = dalAuth.InsertUserPrivilege(objPri)
                            If Not rslt Then Exit For
                        Next
                    End If
                End If
            End If

            If rslt Then
                dalAuth.Commit()
                Return True
            Else
                dalAuth.RollBack()
                Return False
            End If
        Catch ex As Exception
            Try
                dalAuth.RollBack()
            Catch
            End Try
            Throw ex
        End Try
    End Function

End Class
